/**
 * 
 */
package com.fruit.app.service.impl;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fruit.app.dao.BounsItemMapper;
import com.fruit.app.dao.BounsMapper;
import com.fruit.app.model.Bouns;
import com.fruit.app.model.BounsExample;
import com.fruit.app.model.BounsExample.Criteria;
import com.fruit.app.model.BounsItem;
import com.fruit.app.model.BounsItemExample;
import com.fruit.app.service.BounsService;

/**
 * @author jin
 *
 */
@Service
public class BounsServiceImpl implements BounsService {
	
	@Autowired
	private BounsMapper bounsMapper;
	@Autowired
	private BounsItemMapper bounsItemMapper;

	@Override
	public Bouns queryBounsBySaleMember(Bouns bouns) {
		BounsExample param = new BounsExample();
		param.createCriteria().andSalePeriodEqualTo(bouns.getSalePeriod()).andMemberCodeEqualTo(bouns.getMemberCode());
		List<Bouns> bounsList = bounsMapper.selectByExample(param);
		if (CollectionUtils.isEmpty(bounsList)) {
			return new Bouns();
		}else{
			if (bounsList.size() > 1) {
				throw new RuntimeException("too many result");
			}
		}
		return bounsList.get(0);
	}

	@Override
	public List<Bouns> queryByObject(String time, String memberCname, Integer level, final BounsExample bounsExample) {
		Criteria condition = bounsExample.createCriteria();
		
		if(BooleanUtils.isFalse(StringUtils.isBlank(time))){
			condition.andSalePeriodEqualTo(time);
		}else{
			Calendar calendar = Calendar.getInstance();
			condition.andSalePeriodLike(calendar.get(Calendar.YEAR) + "%");
		}
		if(BooleanUtils.isFalse(StringUtils.isBlank(memberCname))){
			condition.andMemberCnameLike("%" + memberCname + "%");
		}
		if(level != null){
			condition.andJxsLevelEqualTo(BigDecimal.valueOf(level));
		}
		bounsExample.setOrderByClause("jxs_level,sale_period desc");
		List<Bouns> bounsList = bounsMapper.selectByExample(bounsExample);
		if (CollectionUtils.isEmpty(bounsList)) {
			return Collections.emptyList();
		}
		return bounsList;
	}
	
	public Integer getCount(String time, String memberCname, Integer level){
		BounsExample param = new BounsExample();
		Criteria condition = param.createCriteria();
		if(BooleanUtils.isFalse(StringUtils.isBlank(time))){
			condition.andSalePeriodEqualTo(time);
		}
		if(BooleanUtils.isFalse(StringUtils.isBlank(memberCname))){
			condition.andMemberCnameLike("%" + memberCname + "%");
		}
		if(level != null){
			condition.andJxsLevelEqualTo(BigDecimal.valueOf(level));
		}
		return bounsMapper.countByExample(param);
	}

	@Override
	public List<BounsItem> queryItemBySaleMember(BounsItem item) {
		BounsItemExample param = new BounsItemExample();
		param.createCriteria().andSalePeriodEqualTo(item.getSalePeriod()).andMemberCodeEqualTo(item.getMemberCode());
		List<BounsItem> itemList = bounsItemMapper.selectByExample(param);
		if (CollectionUtils.isEmpty(itemList)) {
			return Collections.emptyList();
		}
		return itemList;
	}
	
}
